package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntPhbsjBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntPhbsjBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntPhbsjsyjcjlBsInfoBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntPhbsjsyjcjlBsInfoVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntPhbsjsyjcjlBsInfo;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntPhbsjsyjcjlBsInfoMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntPhbsjsyjcjlBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 普通混凝土配合比设计试验检测记录Service业务层处理
 *
 * @author zxs
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntPhbsjsyjcjlBsInfoServiceImpl implements IReportSnhntPhbsjsyjcjlBsInfoService {

    private final ReportSnhntPhbsjsyjcjlBsInfoMapper baseMapper;

    private final ReportSnhntPhbsjBaDetailMapper detailMapper;

    /**
     * 查询普通混凝土配合比设计试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportSnhntPhbsjsyjcjlBsInfoVo queryById(Long excelId){
        ReportSnhntPhbsjsyjcjlBsInfoVo vo = baseMapper.selectVoById(excelId);
        LambdaQueryWrapper<ReportSnhntPhbsjBaDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ReportSnhntPhbsjBaDetail::getExcelId,excelId);
        List<ReportSnhntPhbsjBaDetail> detailList = detailMapper.selectList(wrapper);
        if (CollUtil.isNotEmpty(detailList)) {
            vo.setDetailList(detailList);
        }
        return vo;
    }

    /**
     * 查询普通混凝土配合比设计试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportSnhntPhbsjsyjcjlBsInfoVo> queryPageList(ReportSnhntPhbsjsyjcjlBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntPhbsjsyjcjlBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntPhbsjsyjcjlBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询普通混凝土配合比设计试验检测记录列表
     */
    @Override
    public List<ReportSnhntPhbsjsyjcjlBsInfoVo> queryList(ReportSnhntPhbsjsyjcjlBsInfoBo bo) {
        LambdaQueryWrapper<ReportSnhntPhbsjsyjcjlBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntPhbsjsyjcjlBsInfo> buildQueryWrapper(ReportSnhntPhbsjsyjcjlBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntPhbsjsyjcjlBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSnhntPhbsjsyjcjlBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSnhntPhbsjsyjcjlBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportSnhntPhbsjsyjcjlBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportSnhntPhbsjsyjcjlBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportSnhntPhbsjsyjcjlBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportSnhntPhbsjsyjcjlBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), ReportSnhntPhbsjsyjcjlBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportSnhntPhbsjsyjcjlBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportSnhntPhbsjsyjcjlBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportSnhntPhbsjsyjcjlBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportSnhntPhbsjsyjcjlBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangplysj() != null, ReportSnhntPhbsjsyjcjlBsInfo::getYangplysj, bo.getYangplysj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportSnhntPhbsjsyjcjlBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportSnhntPhbsjsyjcjlBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportSnhntPhbsjsyjcjlBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportSnhntPhbsjsyjcjlBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportSnhntPhbsjsyjcjlBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportSnhntPhbsjsyjcjlBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportSnhntPhbsjsyjcjlBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getShejqddj()), ReportSnhntPhbsjsyjcjlBsInfo::getShejqddj, bo.getShejqddj());
        lqw.eq(StringUtils.isNotBlank(bo.getBanzff()), ReportSnhntPhbsjsyjcjlBsInfo::getBanzff, bo.getBanzff());
        lqw.eq(StringUtils.isNotBlank(bo.getShejtld()), ReportSnhntPhbsjsyjcjlBsInfo::getShejtld, bo.getShejtld());
        lqw.eq(StringUtils.isNotBlank(bo.getChengxff()), ReportSnhntPhbsjsyjcjlBsInfo::getChengxff, bo.getChengxff());
        lqw.eq(StringUtils.isNotBlank(bo.getBiaozc()), ReportSnhntPhbsjsyjcjlBsInfo::getBiaozc, bo.getBiaozc());
        lqw.eq(StringUtils.isNotBlank(bo.getYanghfs()), ReportSnhntPhbsjsyjcjlBsInfo::getYanghfs, bo.getYanghfs());
        lqw.eq(StringUtils.isNotBlank(bo.getShipqd()), ReportSnhntPhbsjsyjcjlBsInfo::getShipqd, bo.getShipqd());
        lqw.eq(StringUtils.isNotBlank(bo.getYanghhjwd()), ReportSnhntPhbsjsyjcjlBsInfo::getYanghhjwd, bo.getYanghhjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getYanghhjsd()), ReportSnhntPhbsjsyjcjlBsInfo::getYanghhjsd, bo.getYanghhjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getShuinqddj()), ReportSnhntPhbsjsyjcjlBsInfo::getShuinqddj, bo.getShuinqddj());
        lqw.eq(StringUtils.isNotBlank(bo.getShuincd()), ReportSnhntPhbsjsyjcjlBsInfo::getShuincd, bo.getShuincd());
        lqw.eq(StringUtils.isNotBlank(bo.getCujlcd()), ReportSnhntPhbsjsyjcjlBsInfo::getCujlcd, bo.getCujlcd());
        lqw.eq(StringUtils.isNotBlank(bo.getCujlbgmd()), ReportSnhntPhbsjsyjcjlBsInfo::getCujlbgmd, bo.getCujlbgmd());
        lqw.eq(StringUtils.isNotBlank(bo.getGuigzl1()), ReportSnhntPhbsjsyjcjlBsInfo::getGuigzl1, bo.getGuigzl1());
        lqw.eq(StringUtils.isNotBlank(bo.getGuigzl2()), ReportSnhntPhbsjsyjcjlBsInfo::getGuigzl2, bo.getGuigzl2());
        lqw.eq(StringUtils.isNotBlank(bo.getGuigzl3()), ReportSnhntPhbsjsyjcjlBsInfo::getGuigzl3, bo.getGuigzl3());
        lqw.eq(StringUtils.isNotBlank(bo.getGuigzl4()), ReportSnhntPhbsjsyjcjlBsInfo::getGuigzl4, bo.getGuigzl4());
        lqw.eq(StringUtils.isNotBlank(bo.getBil1()), ReportSnhntPhbsjsyjcjlBsInfo::getBil1, bo.getBil1());
        lqw.eq(StringUtils.isNotBlank(bo.getBil2()), ReportSnhntPhbsjsyjcjlBsInfo::getBil2, bo.getBil2());
        lqw.eq(StringUtils.isNotBlank(bo.getBil3()), ReportSnhntPhbsjsyjcjlBsInfo::getBil3, bo.getBil3());
        lqw.eq(StringUtils.isNotBlank(bo.getBil4()), ReportSnhntPhbsjsyjcjlBsInfo::getBil4, bo.getBil4());
        lqw.eq(StringUtils.isNotBlank(bo.getXijlcd()), ReportSnhntPhbsjsyjcjlBsInfo::getXijlcd, bo.getXijlcd());
        lqw.eq(StringUtils.isNotBlank(bo.getXijlbgmd()), ReportSnhntPhbsjsyjcjlBsInfo::getXijlbgmd, bo.getXijlbgmd());
        lqw.eq(StringUtils.isNotBlank(bo.getXidms()), ReportSnhntPhbsjsyjcjlBsInfo::getXidms, bo.getXidms());
        lqw.eq(StringUtils.isNotBlank(bo.getXijllx()), ReportSnhntPhbsjsyjcjlBsInfo::getXijllx, bo.getXijllx());
        lqw.eq(StringUtils.isNotBlank(bo.getChanjlmc1()), ReportSnhntPhbsjsyjcjlBsInfo::getChanjlmc1, bo.getChanjlmc1());
        lqw.eq(StringUtils.isNotBlank(bo.getChanjlyl1()), ReportSnhntPhbsjsyjcjlBsInfo::getChanjlyl1, bo.getChanjlyl1());
        lqw.eq(StringUtils.isNotBlank(bo.getChanjlmc2()), ReportSnhntPhbsjsyjcjlBsInfo::getChanjlmc2, bo.getChanjlmc2());
        lqw.eq(StringUtils.isNotBlank(bo.getChanjlyl2()), ReportSnhntPhbsjsyjcjlBsInfo::getChanjlyl2, bo.getChanjlyl2());
        lqw.eq(StringUtils.isNotBlank(bo.getFenmhmc()), ReportSnhntPhbsjsyjcjlBsInfo::getFenmhmc, bo.getFenmhmc());
        lqw.eq(StringUtils.isNotBlank(bo.getFenmhcd()), ReportSnhntPhbsjsyjcjlBsInfo::getFenmhcd, bo.getFenmhcd());
        lqw.eq(StringUtils.isNotBlank(bo.getFenmhjb()), ReportSnhntPhbsjsyjcjlBsInfo::getFenmhjb, bo.getFenmhjb());
        lqw.eq(StringUtils.isNotBlank(bo.getFenmhcl()), ReportSnhntPhbsjsyjcjlBsInfo::getFenmhcl, bo.getFenmhcl());
        lqw.eq(StringUtils.isNotBlank(bo.getKuangfmc()), ReportSnhntPhbsjsyjcjlBsInfo::getKuangfmc, bo.getKuangfmc());
        lqw.eq(StringUtils.isNotBlank(bo.getKuangfcd()), ReportSnhntPhbsjsyjcjlBsInfo::getKuangfcd, bo.getKuangfcd());
        lqw.eq(StringUtils.isNotBlank(bo.getKuangfcl()), ReportSnhntPhbsjsyjcjlBsInfo::getKuangfcl, bo.getKuangfcl());
        lqw.eq(StringUtils.isNotBlank(bo.getWaicjmc1()), ReportSnhntPhbsjsyjcjlBsInfo::getWaicjmc1, bo.getWaicjmc1());
        lqw.eq(StringUtils.isNotBlank(bo.getChanl1()), ReportSnhntPhbsjsyjcjlBsInfo::getChanl1, bo.getChanl1());
        lqw.eq(StringUtils.isNotBlank(bo.getWaicjmc2()), ReportSnhntPhbsjsyjcjlBsInfo::getWaicjmc2, bo.getWaicjmc2());
        lqw.eq(StringUtils.isNotBlank(bo.getChanl2()), ReportSnhntPhbsjsyjcjlBsInfo::getChanl2, bo.getChanl2());
        lqw.eq(StringUtils.isNotBlank(bo.getWaicjmc3()), ReportSnhntPhbsjsyjcjlBsInfo::getWaicjmc3, bo.getWaicjmc3());
        lqw.eq(StringUtils.isNotBlank(bo.getChanl3()), ReportSnhntPhbsjsyjcjlBsInfo::getChanl3, bo.getChanl3());
        lqw.eq(bo.getSsyrq() != null, ReportSnhntPhbsjsyjcjlBsInfo::getSsyrq, bo.getSsyrq());
        lqw.eq(bo.getQsyrq() != null, ReportSnhntPhbsjsyjcjlBsInfo::getQsyrq, bo.getQsyrq());
        lqw.eq(bo.getEsyrq() != null, ReportSnhntPhbsjsyjcjlBsInfo::getEsyrq, bo.getEsyrq());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportSnhntPhbsjsyjcjlBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportSnhntPhbsjsyjcjlBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportSnhntPhbsjsyjcjlBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportSnhntPhbsjsyjcjlBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, ReportSnhntPhbsjsyjcjlBsInfo::getRiq, bo.getRiq());
        return lqw;
    }

    /**
     * 新增普通混凝土配合比设计试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportSnhntPhbsjsyjcjlBsInfoBo bo) {
        ReportSnhntPhbsjsyjcjlBsInfo add = BeanUtil.toBean(bo, ReportSnhntPhbsjsyjcjlBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            List<ReportSnhntPhbsjBaDetail> detailList = new ArrayList<>();
            for (int i = 0; i <= 2; i++) {
                ReportSnhntPhbsjBaDetail detail = new ReportSnhntPhbsjBaDetail();
                detail.setExcelId(bo.getExcelId());
                detailList.add(detail);
            }
            detailMapper.insertBatch(detailList);
        }
        return flag;
    }

    /**
     * 修改普通混凝土配合比设计试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportSnhntPhbsjsyjcjlBsInfoBo bo) {
        ReportSnhntPhbsjsyjcjlBsInfo update = BeanUtil.toBean(bo, ReportSnhntPhbsjsyjcjlBsInfo.class);
        validEntityBeforeSave(update);
        if(bo.getDetailList()!=null&&bo.getDetailList().size()!=0){
            detailMapper.updateBatchById(bo.getDetailList());
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntPhbsjsyjcjlBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除普通混凝土配合比设计试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
